package com.ecity.util {
	import flash.display.Loader;
	import flash.events.Event;
	import flash.geom.Rectangle;
	import flash.net.FileReference;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import flash.display.NativeWindow;
	import flash.display.NativeWindowInitOptions;
	import flash.display.NativeWindowSystemChrome;
	import flash.display.NativeWindowType;
	import flash.display.Screen;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.*;
	/**
	 * ...
	 * @author yicheng.zhang
	 */
	public class GameLoader {
		private var _currentCfgURL:String;
		private var _callBack:Function;
		private var urlmap:HashMap;
		private var initURLMap:HashMap;
		public var cfgXml:XML;
		public static var totalSize:int;
		private var keys:Array;
		
		private var isInit:Boolean;
		
		public static const INIT_UI:String = "init";
		public static const PROGRESS:String = "progress";
		private static const LIST_UI:String = "serial";
		
		public function GameLoader() {
			urlmap = new HashMap();
			initURLMap = new HashMap();
		}
		public function startLoad(caller:Function):void {
			_callBack = caller;
			loadConfiger("config/Game.xml");
		}
		
		private function loadConfiger(url:String):void{
			this._currentCfgURL = url;
			var loader:URLLoader = new URLLoader();
			var req:URLRequest = new URLRequest(url);
			loader.load(req);
			loader.addEventListener(Event.COMPLETE, onConfigerFileLoaded);
		}
		
		private function onConfigerFileLoaded(e:Event):void {
			var cxml:XML = new XML(e.target.data);
			UIFactory.putConfiger("Configer",cxml);
			this.cfgXml = cxml;
			creatURLMap(cxml);
		}
		
		private function creatURLMap(cxml:XML):void {
			for each(var item:XML in cxml.asset.children()){
				var key:String = item.name();
				assortURL(cxml,key);
			}
		}
		
		private function assortURL(cxml:XML,key:String):void {
			if (key == LIST_UI) {
				getListURL(cxml);
			}else {
				if (key == INIT_UI) {
					for each(var item:XML in cxml.asset[key].children()){
						initURLMap.put(item.@id, item.@url);
					}
					keys = initURLMap.keys();
					isInit = true;
					loadAsset();
				}else{
					for each(var node:XML in cxml.asset[key].children()){
						urlmap.put(node.@id, node.@url);
					}
				}
			}
		}
		
		private function getMainAsset():void {
			keys = urlmap.keys();
			totalSize = urlmap.size();
			loadAsset();
		}
		
		private function getListURL(cxml:XML):void {
			for each(var node:XML in cxml.asset[LIST_UI].children()){
				var length:int = int(node.@end) -int(node.@start)+1;
				for (var i:int = 0; i < length; i++ ) {
					var regExp:RegExp = /\*/g;
					var url:String = node.@url;
					var num:String;
					if (i.toString().length <node.@bitCount) {
						var zeroStr:String = "";
						for (var t:int = 0; t < node.@bitCount-1; t++) {
							zeroStr += "0";
						}
						num = zeroStr + i;
					}else {
						num = i.toString();
					}
					var idKey:String = node.@id+"_"+(i+1);
					url = url.replace(regExp, num);
					urlmap.put(idKey, url);
				}
			}
		}

		
		
		/**
		 * 加载资源的函数
		 * @param url   通配加载地址
		 * 
		 */
		private function loadAsset():void {
			var url:String;
			if (isInit) {
				url = initURLMap.get(keys[0]);
			}else {
				url = urlmap.get(keys[0]);
			}
			var mRequest:URLRequest = new URLRequest(url);
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
			loader.load(mRequest);
		}
		
		private function onCompleteHandler(e:Event):void {
			var name:String = keys[0];
			var loader:Loader = Loader( e.target.loader );
			UIFactory.put(name, loader.content,e.target.contentType);		
			if (keys.length == 1) {
				if (isInit) {
					_callBack.apply(null, [INIT_UI]);
					trace("))))))) ",UIFactory.uimap.keys());
					isInit = false;
					getMainAsset();
				}else {
					//trace("--->",UIFactory.uimap.keys());
					_callBack.apply(null,["Asset"]);
				}	
			}else {
				keys.shift();
				if(!isInit){
					_callBack.apply(null, ["progress"]);
				}
				this.loadAsset();
			}
		}

		
	}

}